<template>
  <!--用户管理-->
  <div class="search">
    <f-drawer :title="modalTitle" ref="drawer" :width="900" @submit="handleSubmit" @visibleChange="visibleChange">
      <a-tabs @tabClick="handleTabClick" :defaultActiveKey="current" v-model="current">
        <a-tab-pane tab="用户信息" key="form1">
          <user-form ref="form1" v-if="drawerVisible"></user-form>
        </a-tab-pane>
        <a-tab-pane tab="修改密码" key="form2" v-if="isEdit">
          <update-password ref="form2" v-if="drawerVisible"></update-password>
        </a-tab-pane>
      </a-tabs>
    </f-drawer>
  </div>
</template>
<script>

  import UserForm from './user_form'
  import UpdatePassword from './update_password'

  export default {
    name: 'userModal',
    props: {},
    data() {
      return {
        saving: false,
        isEdit: false,
        title: '',
        drawerVisible:false,
        current: 'form1',
        userName: '',
        formData: {},
        forms: [
          'form1',
          'form2'
        ]
      }
    },
    methods: {
      handleShow(data) {
        if (data && data.userId) {
          this.isEdit = true
          this.userName = data.userName
          this.formData = data
        } else {
          this.formData = {}
        }
        this.$refs.drawer.show()
        this.handleTabClick('form1')
      },
      visibleChange(drawerVisible) {
        this.drawerVisible = drawerVisible
      },
      handleSubmit(callback) {
        this.$refs[this.current].handleSubmit(callback)
      },
      handleTabClick(name) {
        this.current = name
        if (this.current === this.forms[0]) {
          this.title = this.isEdit ? '编辑用户 - ' : '添加用户'
        }
        if (this.current === this.forms[1]) {
          this.title = '修改密码-'
        }
        this.$nextTick(() => {
          this.$refs[this.current].setData(this.formData)
        })
      }
    },
    computed: {
      modalTitle: function() {
        return this.title + this.userName
      }
    },
    created() {

    },
    components: { UpdatePassword, UserForm }
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
